using System;
using System.Collections.Generic;
using System.Text;


namespace Agatha.ServiceLayer
{
    public static class ExceptionFactory
    {
        public static DataAccessException CreateDataAccessException(string message)
        {
            return CreateDataAccessException(message, (Exception)null);
        }

        public static BusinessException CreateBusinessException(string message)
        {
            return CreateBusinessException(message, (Exception)null);
        }

        public static DataAccessException CreateDataAccessException(string message, params object[] args)
        {
            return CreateDataAccessException(string.Format(message, args));
        }

        public static BusinessException CreateBusinessException(string message, params object[] args)
        {
            return CreateBusinessException(string.Format(message, args));
        }

        public static DataAccessException CreateDataAccessException(string message, Exception innerException)
        {
            DataAccessException dataAccessException = new DataAccessException(message, innerException);

            dataAccessException.AddMessage(message);

            return dataAccessException;
        }

        public static BusinessException CreateBusinessException(string message, Exception innerException)
        {
            var businessException = new BusinessException(message, innerException);
            return businessException;
        }

        public static DataAccessException CreateDataAccessException(List<string> messages)
        {
            return CreateDataAccessException(messages, null);
        }

        public static DataAccessException CreateDataAccessException(List<string> messages, Exception innerException)
        {
            DataAccessException dataAccessException = new DataAccessException(ParseAllExceptionsMessages(messages), innerException);

            foreach (string exceptionMessage in messages)
            {
                dataAccessException.AddMessage(exceptionMessage);
            }

            return dataAccessException;
        }

        private static string ParseAllExceptionsMessages(IEnumerable<string> dataExceptionMessages)
        {
            var sb = new StringBuilder();

            foreach (var dataExceptionMessage in dataExceptionMessages)
            {
                sb.Append(dataExceptionMessage);
            }

            return sb.ToString();
        }
    }
}